package sun.sundy.functiontest.utils.zxing;

import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.UUID;

/**
 * Created by 14033165 on 2017/7/20.
 */

public class MyUtils {
    public MyUtils() {
    }

    public static byte[] toByteArray(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        try {
            ObjectOutputStream ex = new ObjectOutputStream(bos);
            ex.writeObject(obj);
            ex.flush();
            bytes = bos.toByteArray();
            ex.close();
            bos.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return bytes;
    }

    public static Object toObject(byte[] bytes) {
        Object obj = null;

        try {
            ByteArrayInputStream ex = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(ex);
            obj = ois.readObject();
            ois.close();
            ex.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        } catch (ClassNotFoundException var5) {
            var5.printStackTrace();
        }

        return obj;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String Double2Int(String doubleNum) {
        return (int) Double.parseDouble(doubleNum) + "";
    }

    public static int String2Int(String intNum) {
        return TextUtils.isEmpty(intNum) ? 0 : Integer.parseInt(intNum);
    }

    public static long String2Long(String longNum) {
        return TextUtils.isEmpty(longNum) ? 0L : Long.parseLong(longNum);
    }

    public static boolean isSupportCameraLedFlash(PackageManager pm) {
        if (pm != null) {
            FeatureInfo[] features = pm.getSystemAvailableFeatures();
            if (features != null) {
                FeatureInfo[] var2 = features;
                int var3 = features.length;

                for (int var4 = 0; var4 < var3; ++var4) {
                    FeatureInfo f = var2[var4];
                    if (f != null && "android.hardware.camera.flash".equals(f.name)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public static boolean containsEmoji(String str) {
        int len = str.length();

        for (int i = 0; i < len; ++i) {
            if (isEmojiCharacter(str.charAt(i))) {
                return true;
            }
        }

        return false;
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return codePoint != 0 && codePoint != 9 && codePoint != 10 && codePoint != 13 && (codePoint < 32 || codePoint > '\ud7ff') && (codePoint < '\ue000' || codePoint > '�') && (codePoint < 65536 || codePoint > 1114111);
    }
}
